from PySide2.QtGui import *
from PySide2.QtWidgets import *

from api.api_project import update_contracts_file
from api.api_view import update_tb_contract
from common.config import Contract_Excel_path
from common.pd import get_col_values
from api.api_project import public_data, get_contract_df
from common.pyside2_table.qtable import Base_Table

from common.pyside2_table.qt_wgt import Th_Base, show_msg, open_path
from view.table_col_list import contract_col_model_list


class View_Contract(QWidget):
    def __init__(self):
        super().__init__()
        self.view_name = "tab_contract"
        self.data = public_data
        self.setupUi()
        self.bind()
        self.update_cmb()

    def setupUi(self):
        """设置合同页面控件"""
        # region 布局
        # 整体布局为垂直布局
        lt_pg = QVBoxLayout(self)
        fm_ctrl = QFrame()  # 操作栏
        lt_pg.addWidget(fm_ctrl)
        lt_header = QHBoxLayout(fm_ctrl)

        # endregion

        # region 操作栏
        # 左侧
        self.btn_open_contract = QPushButton("打开合同表")
        # self.btn_open_contract.setMinimumSize(QSize(40, 0))
        self.btn_update_contract = QPushButton("合同更新")


        # 中间，合同选项
        # 项目cmb

        self.ck_has_delivery = QCheckBox(text="隐藏完工合同")

        left_spacer = QSpacerItem(120, 20, QSizePolicy.Fixed, QSizePolicy.Minimum)

        # right_spacer = QSpacerItem(50, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
        self.btn_reset_contract_info = QPushButton("重置合同信息")
        self.btn_save_contract_info = QPushButton("保存合同更改")
        end_spacer = QSpacerItem(40, 20, QSizePolicy.Fixed, QSizePolicy.Minimum)
        self.btn_file = QPushButton("更新文函")

        lt_header.addWidget(self.btn_open_contract)
        lt_header.addWidget(self.btn_update_contract)
        lt_header.addItem(left_spacer)
        # wgt_list_left = [self.lb_project, self.cmb_project, self.lb_lot, self.cmb_lot_code, self.lb_factory,
        #                  self.cmb_factory, self.ck_has_delivery]
        # for item in wgt_list_left:
        #     lt_header.addWidget(item)
        # lt_header.addItem(right_spacer)
        lt_header.addWidget(self.btn_reset_contract_info)
        lt_header.addWidget(self.btn_save_contract_info)
        lt_header.addItem(end_spacer)
        lt_header.addWidget(self.btn_file)

        # endregion

        # region 信息展示标签和表格
        self.lb_message = QLabel(text="尚未授权访AED")
        self.lb_message.setAutoFillBackground(False)
        self.lb_message.setWordWrap(True)
        lt_pg.addWidget(self.lb_message, 0, Qt.AlignLeft)
        self.tb_contract = Base_Table("contract",contract_col_model_list)
        lt_pg.addWidget(self.tb_contract)
        self.tb_contract.reset_data()

        # endregion

    def bind(self):
        self.btn_file.clicked.connect(lambda: update_contracts_file())
        self.btn_open_contract.clicked.connect(self.open_contract_excel)
        self.btn_update_contract.clicked.connect(self.update_contract)
        # self.ck_has_delivery.clicked.connect(self.cmb_change)

    def update_contract(self):
        update_tb_contract()
        self.data.Df_Contract = get_contract_df()
        self.data.Df_Show_Contract = self.data.Df_Contract
        self.tb_contract.reset_tb_data(self.data.Df_Contract)
        show_msg("合同已更新", "blue", self.lb_message)
        self.btn_update_contract.setEnabled(False)

    def open_contract_excel(self):
        open_path(Contract_Excel_path)
        self.btn_update_contract.setEnabled(True)

    def update_cmb(self, is_all=True):
        if is_all:
            df = self.data.Df_Contract
        else:
            df = self.data.Df_Show_Contract
        project_list = get_col_values(df, "project", "所有")
        lot_code_list = get_col_values(df, "lot_code", "所有")
        factory_list = get_col_values(df, "factory", "所有")
        self.data.Project_list = project_list
        self.data.Factory_list = factory_list
        self.data.Lot_list = lot_code_list

    def update_contract_file(self):
        th_update_file = Th_Update_File()
        th_update_file.start()
        th_update_file.signal_send.connect(self.show_dialog)

    def show_dialog(self, value):
        QMessageBox.information(self, title="合同文函更新", text=value)


class Th_Update_File(Th_Base):

    def run(self):
        result = update_contracts_file()
        self.signal_send.emit(result)
